﻿Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO

<Serializable()> _
Public Class ZuneDatabase

    Public dbFile As String

    Public Artists As SerializableDictionary(Of String, ZuneArtist)
    Public Images As SerializableDictionary(Of String, ZuneImage)

    Public Sub New()
        Artists = New SerializableDictionary(Of String, ZuneArtist)
        Images = New SerializableDictionary(Of String, ZuneImage)
    End Sub

    Public Shared Function Load(ByVal file As String) As ZuneDatabase
        Dim instance As New ZuneDatabase
        Dim document As New Xml.XmlDocument
        Dim serializer As New XmlSerializer(GetType(ZuneDatabase))

        instance.dbFile = file

        '//If the file is not existant, return empty instance
        If Not IO.File.Exists(file) Then
            Return instance
        ElseIf IO.File.ReadAllText(file) = "" Then
            Return instance
        End If

        Dim fStream As New FileStream(file, FileMode.Open)

        instance = serializer.Deserialize(fStream)

        fStream.Close()

        Return instance

        document.Load(file)

        For Each node As XmlNode In document.DocumentElement.ChildNodes
            Select Case node.Name
                Case "artist"
                    Dim tmpArtist As New ZuneArtist

                    For Each prop As XmlNode In node.ChildNodes
                        Select Case prop.Name
                            Case "name"
                                tmpArtist.name = prop.InnerText
                            Case "uuid"
                                tmpArtist.uuid = prop.InnerText
                            Case "uuidOfficial"
                                tmpArtist.uuidOfficial = prop.InnerText

                        End Select
                    Next

                    instance.AddArtist(tmpArtist)
            End Select
        Next

        Return instance
    End Function

    Public Overloads Function Save() As Boolean
        Return Save(dbFile)
    End Function

    Public Overloads Function Save(ByVal file As String) As Boolean
        Dim writer As XmlWriter
        Dim settings As New XmlWriterSettings

        settings.Indent = True

        If Not IO.File.Exists(file) Then IO.File.Create(file).Close()

        'writer = XmlWriter.Create(file, settings)

        Dim serializer As New XmlSerializer(GetType(ZuneDatabase))
        Dim twriter As New StreamWriter(file)

        serializer.Serialize(twriter, Me)

        'With writer

        '    .WriteStartDocument()
        '    .WriteStartElement("database")

        '    For Each artist As ZuneArtist In Artists.Values
        '        .WriteStartElement("artist")
        '        .WriteElementString("name", artist.name)
        '        .WriteElementString("uuid", artist.uuid)
        '        .WriteElementString("uuidOfficial", artist.uuidOfficial)

        '        .WriteStartElement("images")
        '        For Each img As ZuneImage In artist.images.Values
        '            '.WriteElementString(
        '        Next
        '        .WriteEndElement()

        '        .WriteEndElement()
        '    Next

        '    .WriteEndElement()
        '    .WriteEndDocument()

        'End With

        'writer.Close()
        twriter.Close()

        Return True
    End Function

    Public Function AddArtist(ByVal newArtist As ZuneArtist) As Boolean

        '// Check for duplicates
        For Each artist As String In Artists.Keys
            If newArtist.name = artist Then
                MsgBox("There's already an artist with the same name present.", MsgBoxStyle.Critical, "Adding Artist failed...")
                Return False
            End If
        Next

        Artists.Add(newArtist.name, newArtist)

        Return True
    End Function

    Public Function FindArtistByUUID(ByVal uuid As String) As ZuneArtist
        For Each artist As ZuneArtist In Artists.Values
            If artist.uuid = uuid Then Return artist
        Next

        Return Nothing
    End Function

End Class
